<?php
namespace app\admin\controller;
use app\admin\model\Outsign as thisModel;
use app\admin\model\Equipgroup as equipgroupModel;
use think\Db;
use Env;
class Outsign extends Common
{
    function index(){
        $thisModel = new thisModel();
        $equipgroupModel = new equipgroupModel();
        $mainData = $thisModel->mainData();
        $inputData = input('get.');

        // 分组
        $equipgroup_list = $equipgroupModel->mainAllList();
        $this->assign('equipgroup_list', $equipgroup_list);

     // dump($mainData);exit;
        $this->assign('list',$mainData['list']);
        $this->assign('page',$mainData['page']);
        $this->assign('count',$mainData['count']);
        $this->assign('inputData',$inputData);

        return $this->fetch('index');
    }

    function mainDel(){
        $id = input('id');
        if($id>0){
            $thisModel = new thisModel();
            $one = $thisModel->get($id);
            $res = $one->delete();
            if($res !== false){
                exitjson(1,'删除成功');
            }else{
                exitjson(0,'删除失败');
            }
        }
    }

    function form(){
        $thisModel = new thisModel();
        // $data = $thisModel->disposeData(input('post.'));
        $data['patient_id'] = input('post.patient_id');
        $data['equipgroup_id'] = input('post.equipgroup_id');
        $data['starttime'] = time();

        $equipgroup = Db::name("equip_group")->find($data['equipgroup_id']);
        $minute = $equipgroup['time_min']>0 ? $equipgroup['time_min'] : 0;
        $data['endtime'] = strtotime("+{$minute} minute");

        $res = $thisModel->save($data);
        if($res){
            $this->success('提交成功','Outsign/index','',0);
        }else{
            $this->error('提交失败','Outsign/index','',0);
        }

    }

    function out_sign(){
        if(request()->isAjax()){
            $rfid = trim(input('post.rfid'));
            $patient = Db::name("patient")->where("rfid",$rfid)->order("id desc")->find();
            if(!$patient){
                exitjson('0','无效标签');
            }
            $where = "status=0 AND starttime>0 AND endtime=0 AND rfid='".$rfid."'";
            $out_sign = Db::name("out_sign")->where($where)->order("id desc")->find();
            // 是否有记录
            if($out_sign){
                $data = array(
                    'endtime'   => time(),
                    'status'    => 1,
                    );
                $res = Db::name("out_sign")->where("id",$out_sign['id'])->update($data);
                if($res){
                    exitjson('1',$patient['patient_name'].' 恢复告警');
                }else{
                    exitjson('0','操作失败');
                }
            }else{
                $data = array(
                    'starttime' => time(),
                    'rfid'  => $rfid,
                    'status'    => 0
                    );
                $res = Db::name("out_sign")->insertGetId($data);
                if($res){
                    exitjson('1',$patient['patient_name'].' 消除告警');
                }else{
                    exitjson('0','操作失败');
                }
            }
            
        }
    }
}